package com.kjtpay.design_pattern.flyweight;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.kjtpay.design_pattern.R;
import com.kjtpay.design_pattern.adapter.AdapterActivity;

public class FlyweightActivity extends AppCompatActivity {

    public static void launch(Context context) {
        if (null != context) {
            Intent intent = new Intent();
            intent.setClass(context, FlyweightActivity.class);
            if (!(context instanceof Activity)) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            }
            context.startActivity(intent);
        }
    }

    RadioGroup radioGroupType;
    RadioGroup radioGroupUser;
    TextView textViewUse;
    WebSiteFactory webSiteFactory;
    String message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flyweight);
        setTitle("享元模式");

        radioGroupType = findViewById(R.id.radioGroupType);
        radioGroupUser = findViewById(R.id.radioGroupUser);
        textViewUse = findViewById(R.id.textViewUse);

        webSiteFactory = new WebSiteFactory();
        message = "";
    }

    public void onUseClick(View view) {
        RadioButton typeButton = findViewById(radioGroupType.getCheckedRadioButtonId());
        WebSite webSite = webSiteFactory.getWebSiteCategory(typeButton.getText().toString());
        RadioButton userButton = findViewById(radioGroupUser.getCheckedRadioButtonId());
        message += webSite.use(new User(userButton.getText().toString()));
        textViewUse.setText(message);
    }

    public void onResetButtonClick(View view) {
        message = "";
        textViewUse.setText(message);
    }

    public void onCountClick(View view) {
        message += "得到网站分类总数为：" + webSiteFactory.getWebSiteCount() + "\n";
        textViewUse.setText(message);
    }
}